package com.chat.common.user.controller;


import com.chat.common.common.domain.vo.resp.ApiResult;
import com.chat.common.common.utils.AssertUtil;
import com.chat.common.common.utils.RequestHolder;
import com.chat.common.user.dao.UserDao;
import com.chat.common.user.domain.dto.ItemInfoDTO;
import com.chat.common.user.domain.dto.SummeryInfoDTO;
import com.chat.common.user.domain.entity.User;
import com.chat.common.user.domain.enums.RoleEnum;
import com.chat.common.user.domain.vo.req.BadgeResp;
import com.chat.common.user.domain.vo.req.BlackReq;
import com.chat.common.user.domain.vo.req.modifyNameReq;
import com.chat.common.user.domain.vo.req.user.ItemInfoReq;
import com.chat.common.user.domain.vo.req.user.SummeryInfoReq;
import com.chat.common.user.domain.vo.req.wearingBadgeReq;
import com.chat.common.user.domain.vo.resp.UserInfoResp;
import com.chat.common.user.mapper.UserMapper;
import com.chat.common.user.service.IRoleService;
import com.chat.common.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author <a href="https://www.linxin.cc">lin</a>
 * @since 2024-09-12
 */
@RestController
@RequestMapping("/capi/user")
@Api(tags = "用户管理模块")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private IRoleService iRoleService;


    @ApiOperation("用户信息")
    @GetMapping("/userInfo")
    public ApiResult<UserInfoResp> userInfo() {
        return ApiResult.success(userService.getUserInfo(RequestHolder.get().getUid()));
    }

    @ApiOperation("修改用户名称")
    @PutMapping("/name")
    public ApiResult<Void> modifyUserName(@Valid @RequestBody modifyNameReq modifyNameReq) {
        userService.modifyName(RequestHolder.get().getUid(),modifyNameReq.getName());
        return ApiResult.success();
    }

    @GetMapping("/badges")
    @ApiOperation("/可选徽章列表")
    public ApiResult<List<BadgeResp>> badges(){
        return ApiResult.success(userService.badges(RequestHolder.get().getUid()));
    }


    @PutMapping("/black")
    @ApiOperation("/拉黑用户")
    public ApiResult<Void> wearingBadge(@Valid @RequestBody BlackReq blackReq){
        Long uid = RequestHolder.get().getUid();
        boolean hadPower = iRoleService.hasPower(uid, RoleEnum.ADMIN);
        AssertUtil.isTrue(hadPower,"管理员没权限");
        userService.black(blackReq);
        return ApiResult.success();
    }

    @PostMapping("/public/summary/userInfo/batch")
    @ApiOperation("用户聚合信息-返回的代表需要刷新的")
    public ApiResult<List<SummeryInfoDTO>> getSummeryUserInfo(@Valid @RequestBody SummeryInfoReq req) {
        return ApiResult.success(userService.getSummeryUserInfo(req));
    }

    @PostMapping("/public/badges/batch")
    @ApiOperation("徽章聚合信息-返回的代表需要刷新的")
    public ApiResult<List<ItemInfoDTO>> getItemInfo(@Valid @RequestBody ItemInfoReq req) {
        return ApiResult.success(userService.getItemInfo(req));
    }





}

